﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;

namespace NetManager_Server
{
    public partial class FormDangNhap : Form
    {
        public FormDangNhap()
        {
            InitializeComponent();
        }

        private void FormDangNhap_Load(object sender, EventArgs e)
        {

        }

        private void taikhoan_TextChanged(object sender, EventArgs e)
        {
            if (taikhoan.Text != "")
            {
                this.lbtaikhoan.Text = "";
            }
        }

        private void matkhau_TextChanged(object sender, EventArgs e)
        {
            if (matkhau.Text != "")
            {
                this.lbmatkhau.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lbmatkhau_Click(object sender, EventArgs e)
        {

        }

        private void btdangnhap_Click(object sender, EventArgs e)
        {
            if (taikhoan.Text == "")
            {
                this.lbtaikhoan.Text = "Vui lòng nhập tài khoản !";
                taikhoan.Focus();
            }
            else if (matkhau.Text == "")
            {
                this.lbmatkhau.Text = "Vui lòng nhập vào mật khẩu !";
                matkhau.Focus();
            }
            else
            {
                FormMain fm = (FormMain)this.MdiParent;
                ket_noi kn = new ket_noi();
                function fn = new function();
                kn.Open();
                SqlCommand getdata = new SqlCommand("select taikhoan, matkhau, quyen, manv from DANGNHAP where taikhoan = '"+taikhoan.Text+"'",kn.conn);
                SqlDataReader reader = getdata.ExecuteReader();
                string tk = "", mk = "", q = "", mnv = "";
                while (reader.Read())
                {
                    tk = reader[0].ToString();
                    mk = reader[1].ToString();
                    q = reader[2].ToString();
                    mnv = reader[3].ToString();
                }
                reader.Close();
                kn.Close();
                if (taikhoan.Text == tk && matkhau.Text == mk && q == "Admin")
                {
                    fm.AdminLogin();
                    Program.manv = mnv;
                    this.Close();
                }
                else if (taikhoan.Text == tk && matkhau.Text == mk && q == "Quản lý")
                {
                    fm.ManagerLogin();
                    Program.manv = mnv;
                    this.Close();
                }
            }
        }
    }
}
